package com.boco.webmaster.base;

import java.util.*;
import javax.servlet.http.*;

public class SessionManager {
    private static final String SESSION_MANAGER_TABLE = "sessionManagerTable";

    public static class SessionAttrType{
        public static final String PAMANENT = "PERMANENT";
        public static final String QUERY = "QUERY";
    }

    private static SessionManager instance = new SessionManager();
    private SessionManager() {
    }

    public static SessionManager getInstance(){
        return instance;
    }

    public void saveQueryAttr(HttpServletRequest request, String attrName, Object value){
        saveAttribute(request, SessionAttrType.QUERY, attrName, value);
    }

    public void savePermanentAttr(HttpServletRequest request, String attrName, Object value){
        saveAttribute(request, SessionAttrType.PAMANENT, attrName, value);
    }

    public void saveAttribute(HttpServletRequest request, String sessionAttrType,
                              String attrName, Object value){
        Map sessionAttrTable = getSessionAttrTable(request, sessionAttrType);
        if(attrName != null && attrName.trim().length() > 0){
            sessionAttrTable.put(attrName, "");
            request.getSession().setAttribute(attrName, value);
        }
    }

    public void removeQueryAttrs(HttpServletRequest request){
        removeAttributes(request, SessionAttrType.QUERY);
    }

    public void removeAttributes(HttpServletRequest request, String sessionAttrType){
        Map sessionAttrTable = getSessionAttrTable(request, sessionAttrType);
        Object[] attrNames = sessionAttrTable.keySet().toArray();
        for(int i=0; i<attrNames.length; i++){
            request.getSession().removeAttribute((String)attrNames[i]);
        }
        sessionAttrTable.clear();
    }

    private static Map getSessionAttrTable(HttpServletRequest request, String sessionAttrType){
        HttpSession session = request.getSession();
        Map sessionManagerTable = (Map) session.getAttribute(SESSION_MANAGER_TABLE);
        if(sessionManagerTable == null){
            sessionManagerTable = new Hashtable();
            session.setAttribute(SESSION_MANAGER_TABLE, sessionManagerTable);
        }

        Map sessionAttrTable = (Map) sessionManagerTable.get(sessionAttrType);
        if(sessionAttrTable == null){
            sessionAttrTable = new Hashtable();
            sessionManagerTable.put(sessionAttrType, sessionAttrTable);
        }
        return sessionAttrTable;
    }

    public static void clearSession(HttpServletRequest request){
        HttpSession session = request.getSession();
        Enumeration e = session.getAttributeNames();
        List attrNames = new ArrayList();
        while(e.hasMoreElements()){
            attrNames.add(e.nextElement());
        }

        for(int i=0; i<attrNames.size(); i++){
            session.removeAttribute((String)attrNames.get(i));
        }
    }
}
